<?

require_once ('Zend/Auth.php');
require_once ('Zend/Registry.php');
require_once ('Zend/Auth/Adapter/DbTable.php');

require_once('Zend/Config/Ini.php');

class Gpc_User {

   /*
    * Array of Toon objects to add to user 
    */
   private $toon = array();

   private static $factoryClass = null;

   public function __construct( ) {
      // Add exception here after overwriting it in other classes.
      // This class is never instanciated directly.
   }

   public function hasPermission( $action ) {

   }

   public static function factory ( $forum ) {

      // Insure only one instance of said class is created
      if ( self::$factoryClass === null ) {
         $class = ucfirst($forum);
         $className = "Gpc_User_Adapter_" . $class;
   
         require_once('Gpc/User/Adapter/'.$class.'.php');
         self::$factoryClass = new $className;
      }

      return self::$factoryClass;
   }

   public function authenticate( $user, $passwd ) {

      if ( empty($user) || empty($passwd) ) {
        return false;
      }

      // Runs any salting and crypting necessary
      $this->_setUsername($user);
      $this->_setPassword($passwd);

      // Does the actual authentication
      $result = $this->_authenticate($this->getUsername(), $this->getPassword());

      return $result;
   }

   protected function _authenticate( $user, $passwd ) {

      $db = Zend_Registry::get('db');
      $db->setFetchMode(Zend_Db::FETCH_ASSOC);

      $authAdapter = new Zend_Auth_Adapter_DbTable($db);
      $authAdapter->setTableName( $this->getTableName() )
                  ->setIdentityColumn( $this->getIdentityColumn() )
                  ->setCredentialColumn( $this->getCredentialColumn() );

      $authAdapter->setIdentity($user)
                  ->setCredential($passwd);

      $result = $authAdapter->authenticate();

      // Success, save idendity information into the storage.
      if ( $result->isValid() ) {
        $this->_translateIdentity($authAdapter->getResultRowObject());
        $this->_setCookie();
      }

      return $result;
   }

   public function addToon ( Toon $toon ) {
   }
}
